package com.xwb.config;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean:3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager d){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(d);

        // 设置自定义filter
        Map<String, Filter> filter = new LinkedHashMap<>();
        filter.put("authc", new ShiroFilter());
        bean.setFilters(filter);

        //添加shiro的内置过滤器
        /**
           anon：无需认证就可以访问
           authc：必须认证了才能访问
           user：必须拥有了 记住我 功能才能用
           perms：拥有对某个资源的权限才能访问
           role：拥有某个角色权限才能访问
        */

        //拦截
        Map<String, String> filterChainDefinitionMap=new LinkedHashMap<>();
        //授权(设置权限),正常情况下，没有授权会跳到未授权页面
        filterChainDefinitionMap.put("/swagger-ui.html","anon");
        filterChainDefinitionMap.put("/swagger/**","anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**","anon");
        filterChainDefinitionMap.put("/v2/**","anon");
        filterChainDefinitionMap.put("/static/**", "anon");
        // 不用再对登录接口进行拦截，由自定义filter进行处理
        filterChainDefinitionMap.put("/**", "authc");
       // filterMap.put("/**","anon");

//        filterMap.put("/user/add","authc");
//        filterMap.put("/user/update","authc");
        bean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        //设置登录请求,认证失败跳转
        bean.setLoginUrl("/toLoginPage");
//        //设置未授权页面
        bean.setUnauthorizedUrl("/toLoginPage");

        return bean;
    }

    //DafaultWebSecurityManager:2
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建realm对象，需要自定义类:1
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
